﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.SixAcquaint.Model;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.IO;

public partial class BasicBase_KeyUnit_Service : System.Web.UI.Page
{
    int _limitwidth = 640;
    int _limitheight = 480;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            KeyUnitInfo keyUnitDetails = KeyUnit.GetKeyUnitInfoByID(Request["UID"]);

            hlReturn.ToolTip = "返回" + keyUnitDetails.UnitName;
            hlReturn.NavigateUrl = "InsertKeyUnit.aspx?sid=" + keyUnitDetails.StandardID;
            gvHydrants.PageIndex = 0;
            ListPageContral1.SetFirstPage();
            BindHydrants();
            gvHydrants.PageIndex = 0;
            ListPageContral2.SetFirstPage();
            BindService();
            gvEx.PageIndex = 0;
            ListPageContral3.SetFirstPage();
            BindExtinguishSystem();
            ListPageContral4.SetFirstPage();
            gvOtherWater.PageIndex = 0;
            BindOtherWater();

        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gvHydrants.PageIndex = PageIndex;
        BindHydrants();
    }

    protected void ListPageContral2_PageSizeChange(int pageSize, int PageIndex)
    {
        gvHydrants.PageIndex = PageIndex;
        BindService();
    }

    protected void ListPageContral3_PageSizeChange(int pageSize, int PageIndex)
    {

        gvEx.PageIndex = PageIndex;
        BindExtinguishSystem();
    }

    protected void ListPageContral4_PageSizeChange(int pageSize, int PageIndex)
    {

        gvOtherWater.PageIndex = PageIndex;
        BindOtherWater();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        KeyUnitHydrantInfo keyUnitHy = new KeyUnitHydrantInfo();
        keyUnitHy.KeyUnitID = Request["uid"];
        keyUnitHy.HydrantType = ddlServiceName.SelectedValue;
        keyUnitHy.HydrantCount = int.Parse(txtHydrantCount1.Text);
        keyUnitHy.Pressure = Convert.ToDecimal(txtPressure.Text);
        keyUnitHy.Diameter = Convert.ToDecimal(txtGuanJing.Text);
        keyUnitHy.Location = txtLocation.Text;
        string path = Server.MapPath("pics\\");
        string word = "";
        if (Photo.HasFile)
        {
            string ex = Photo.FileName.Substring(Photo.FileName.LastIndexOf('.') + 1);
            if (ex.ToLower() != "doc")
            {
                lblHy.Text = "提示:上传文档格式错误!";
                return;
            }
            else
            {
                if (Photo.FileContent.Length / 1024 / 1024 > 5)
                {

                    lblHy.Text = "提示:上传文档不可大于5M!";
                    return;
                }
                else
                {
                    //Stream oStream = Photo.PostedFile.InputStream;
                    //System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

                    //int owidth = oImage.Width; //原图宽度
                    //int oheight = oImage.Height; //原图高度

                    //if (owidth > _limitwidth || oheight > _limitheight)
                    //{

                    //    lblHy.Text = "超过允许的图片尺寸(" + _limitwidth.ToString() + "×" + _limitheight.ToString() + ")范围!";
                    //    return;

                    //}//检查是否超出规定尺寸

                    word = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + ex;

                    Photo.SaveAs(path + word);
                    keyUnitHy.WaterSourcePhoto = word;

                }
            }
        }
        else
        {
            lblHy.Text = "请添加word文档!";
            return;
        }
        if (Water.InsertKeyUnitHyDrant(keyUnitHy))
        {
            gvHydrants.PageIndex = 0;
            ListPageContral1.SetFirstPage();
            BindHydrants();
            lblHy.Text = "添加成功";
        }
        else
        {
            lblHy.Text = "添加失败！";
        }
    }

    private void BindHydrants()
    {
        List<KeyUnitHydrantInfo> hydrants = Water.GetKeyUnitHydrantsByKeyUnitID(Request["uid"]);
        ListPageContral1.ItemCount = hydrants.Count;
        gvHydrants.DataSource = hydrants;
        gvHydrants.DataBind();
    }
    protected void gvHydrants_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gvHydrants.DataKeys[e.RowIndex]["HydrantID"].ToString();
        KeyUnitHydrantInfo hydrant = Water.GetHydrantByID(id);
        string path = Server.MapPath("pics\\");
        string photo = path + hydrant.WaterSourcePhoto;
        FileInfo info = new FileInfo(photo);
        info.IsReadOnly = false;
        info.Delete();
        if (Water.DeleteKeyUnitHydrant(id))
        {
            lblHy.Text = "删除成功。";
            BindHydrants();
        }
    }
    protected void ddlServices_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlServices.SelectedItem.Text == "消防水池")
        {
            lblMemo.Text = "总储水量";
        }
        else
            if (ddlServices.SelectedItem.Text == "防排烟系统")
                lblMemo.Text = "排烟量";
            else
                if (ddlServices.SelectedItem.Text == "事故照明")
                    lblMemo.Text = "延续时间";
                else

                    if (ddlServices.SelectedItem.Text == "建筑防火分区")
                    {
                        lblMemo.Text = "分区面积";
                        lblLocation.Text = "分割形式";
                    }
                    else
                    {
                        lblLocation.Text = "分布";
                        lblMemo.Text = "备注";
                    }
        tabSerivce.ActiveTabIndex = 1;


    }
    protected void btnSaveServ_Click(object sender, EventArgs e)
    {
        FireServiceInfo fireService = new FireServiceInfo();
        fireService.KeyUnitID = Request["uid"];
        fireService.ServiceName = ddlServices.SelectedValue;
        fireService.Quantity = int.Parse(txtQuantity.Text);
        fireService.Location = txtServiceLocation.Text;
        fireService.Memo = txtMemo.Text;
        if (KeyUnit.InsertService(fireService))
        {
            lblSev.Text = "添加成功。";
            gvService.PageIndex = 0;
            ListPageContral2.SetFirstPage();
            BindService();
        }
        else
            lblSev.Text = "添加失败！";
    }

    private void BindService()
    {
        List<FireServiceInfo> fireInso = KeyUnit.GetServiceByKeyUnitID(Request["uid"]);
        ListPageContral2.ItemCount = fireInso.Count;
        gvService.DataSource = fireInso;
        gvService.DataBind();
    }
    protected void gvService_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sid = gvService.DataKeys[e.RowIndex]["ServiceID"].ToString();
        if (KeyUnit.DeleteService(sid))
        {
            lblSev.Text = "删除成功。";
            BindService();
        }
    }
    protected void btnSaveEx_Click(object sender, EventArgs e)
    {
        ExtinguishingSystemInfo extSys = new ExtinguishingSystemInfo();
        extSys.KeyUnitID = Request["uid"];
        extSys.FacilityType = ddlExType.SelectedValue;
        extSys.Location = txtExLocation.Text;
        extSys.ProtectiveScope = txtExProc.Text;
        extSys.Quantity = int.Parse(txtExCount.Text);
        extSys.ServiceBehavior = txtExWorkCap.Text;
        extSys.SupplyStrength = Convert.ToDecimal(txtExStrength.Text);

        if (KeyUnit.InsertExtinguishSystem(extSys) > 0)
        {
            gvEx.PageIndex = 0;
            ListPageContral3.SetFirstPage();
            BindExtinguishSystem();
        }
    }

    private void BindExtinguishSystem()
    {
        List<ExtinguishingSystemInfo> exs = KeyUnit.GetExtinguishSystemByKeyUnitID(Request["uid"]);
        ListPageContral3.ItemCount = exs.Count();
        gvEx.DataSource = exs;
        gvEx.DataBind();
    }
    protected void btnSaveOtherWater_Click(object sender, EventArgs e)
    {
        OtherWaterSourceInfo otherWater = new OtherWaterSourceInfo();
        otherWater.KeyUnitID = Request["uid"];
        otherWater.Location = txtOWSLocation.Text;
        otherWater.Type = txtOWSType.Text;
        if (KeyUnit.InsertOtherWater(otherWater) > 0)
        {
            gvOtherWater.PageIndex = 0;
            ListPageContral4.SetFirstPage();
            BindOtherWater();
        }
    }

    private void BindOtherWater()
    {
        List<OtherWaterSourceInfo> Waters = KeyUnit.GetOtherWaterByKeyUnitID(Request["uid"]);
        ListPageContral4.ItemCount = Waters.Count;
        gvOtherWater.DataSource = Waters;
        gvOtherWater.DataBind();
    }
    protected void gvEx_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gvEx.DataKeys[e.RowIndex].Value.ToString();
       
        if (KeyUnit.DeleteExtinguishSystem(id))
            BindExtinguishSystem();
    }
    protected void gvOtherWater_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (KeyUnit.DeleteOtherWater(gvOtherWater.DataKeys[e.RowIndex].Value.ToString()))
        {
            BindOtherWater();
        }
    }
    protected void gvHydrants_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label lblID = e.Row.FindControl("lblID") as System.Web.UI.WebControls.Label;
            lblID.Text = Convert.ToString(gvHydrants.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            HyperLink HyPhoto = e.Row.FindControl("HyPhoto") as HyperLink;

            KeyUnitHydrantInfo keyUnitHy = e.Row.DataItem as KeyUnitHydrantInfo;
            HyPhoto.NavigateUrl = "pics/" + keyUnitHy.WaterSourcePhoto;
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void gvService_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label lblID = e.Row.FindControl("lblID") as System.Web.UI.WebControls.Label;
            lblID.Text = Convert.ToString(gvService.PageSize * ListPageContral2.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void gvEx_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            System.Web.UI.WebControls.Label lblID = e.Row.FindControl("lblID") as System.Web.UI.WebControls.Label;
            lblID.Text = Convert.ToString(gvEx.PageSize * ListPageContral3.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    protected void gvOtherWater_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            System.Web.UI.WebControls.Label lblID = e.Row.FindControl("lblID") as System.Web.UI.WebControls.Label;
            lblID.Text = Convert.ToString(gvOtherWater.PageSize * ListPageContral4.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}